package com.zmn.plat.business.impl.orderconfig.track.category;

import java.util.Date;
import java.util.List;
import java.util.Objects;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.string.InitialUtil;
import com.zmn.plat.business.interfaces.orderconfig.track.category.OrderTrackCategoryBService;
import com.zmn.plat.common.dictionary.PlatErrorCodeDict;
import com.zmn.plat.common.dto.orderconfig.track.category.OrderTrackCategoryDRO;
import com.zmn.plat.common.exception.PlatException;
import com.zmn.plat.model.entity.orderconfig.track.category.OrderTrackCategory;
import com.zmn.plat.model.entity.orderconfig.track.category.OrderTrackCategoryQuery;
import com.zmn.plat.services.interfaces.orderconfig.track.category.OrderTrackCategoryService;
import com.zmn.plat.utils.bean.BeanCopyUtils;

/**
 * @author tanbiao
 * @description
 * @modifier
 * @since 2020/11/17 16:12
 **/
@Service
public class OrderTrackCategoryBServiceImpl implements OrderTrackCategoryBService {
    @Resource
    private OrderTrackCategoryService orderTrackCategoryService;

    @Override
    public List<OrderTrackCategoryDRO> listByStatus(Integer status) {
        OrderTrackCategoryQuery query = new OrderTrackCategoryQuery();
        query.setStatus(status);
        List<OrderTrackCategory> list = orderTrackCategoryService.listByQuery(query);
        if (CollectionUtils.isEmpty(list)) {
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_RESULT_NULL);
        }

        List<OrderTrackCategoryDRO> result = BeanCopyUtils.copyProperties(list, OrderTrackCategory.class, OrderTrackCategoryDRO.class, OrderTrackCategoryDRO::new);
        return result;
    }

    @Override
    public Integer addEdit(OrderTrackCategory orderTrackCategory, String operator) {
        Date now = DateUtil.getNow();

        OrderTrackCategory category = orderTrackCategoryService.findByName(orderTrackCategory.getCategName());
        boolean isExist = Objects.nonNull(category);
        // 新增
        if (Objects.isNull(orderTrackCategory.getCategId())) {
            if (isExist) {
                throw new PlatException("名称不能重复");
            }
            orderTrackCategory.setUpdateTime(now);
            orderTrackCategory.setUpdater(operator);
            orderTrackCategory.setFirstLetter(InitialUtil.getFristEnName(orderTrackCategory.getCategName()));
            orderTrackCategory.setCreateTime(now);
            orderTrackCategory.setCreater(operator);
            orderTrackCategory.setStatus(GlobalConsts.YES);
            orderTrackCategoryService.insert(orderTrackCategory);
        } else {
            if (isExist && !Objects.equals(category.getCategId(), orderTrackCategory.getCategId())) {
                throw new PlatException("名称不能重复");
            }
            OrderTrackCategory old = orderTrackCategoryService.findByKey(orderTrackCategory.getCategId());
            old.setFirstLetter(InitialUtil.getFristEnName(orderTrackCategory.getCategName()));
            old.setUpdateTime(now);
            old.setUpdater(operator);
            old.setCategName(orderTrackCategory.getCategName());
            old.setSort(orderTrackCategory.getSort());
            orderTrackCategoryService.updateByKey(old);
        }
        return GlobalConsts.YES;
    }
}
